// index.js

var tcp = wx.createTCPSocket()

Page({

    data: {
        message:'当前还未获得数据',
        listData1:[ {"code":"0","text":"拳头手势(2)",amount:0}, 
                    {"code":"1","text":"食指手势(-2)",amount:0},
                    {"code":"2","text":"OK手势(10)",amount:0}, 
                    {"code":"3","text":"手掌手势(5)",amount:0}, 
                    {"code":"4","text":"yes手势(10)",amount:0},
                    {"code":"5","text":"食指+大拇指(5)",amount:0},
                    {"code":"6","text":"大拇指+小拇指(10)",amount:0} ,
                    {"code":"7","text":"无效值(0)",amount:0}],
        summary_list:[{amount:0,weight:0,atmosphere:0}],
        weight_list:[{"weight":2},{"weight":-2},{"weight":10},{"weight":5},
                     {"weight":10},{"weight":5},{"weight":10},{"weight":0}],
        
      },

onReady: function () {
 
},
onLoad: function () {
    var that = this;
    for (var i = 0; i < that.data.listData1.length; i++) {    
        that.data.listData1[i].amount = 0;
        console.log(' listdata1['+i+'].amount赋初值'+that.data.listData1[i].amount );
      }
      that.data.summary_list[0].amount = 0;
      that.data.summary_list[0].weight = 0;
      that.data.summary_list[0].atmosphere = 0;
    tcp.connect({address: '8.141.164.29', port: 16})
    tcp.onConnect(function(){
        console.log('链接成功')
    })
    
    tcp.onClose(function(){
        console.log('链接关闭')
    })
    tcp.onMessage(function(e){
        
        var buffer = e.message;
        var dataview = new DataView(buffer);
        var ints = new Uint8Array(buffer.byteLength);
        var str = '';
        for (var i = 0; i < ints.length; i++) {
            str += String.fromCharCode(dataview.getUint8(i));
        }
    console.log(str);
    var digit = Number(str);
    console.log(digit);
    for (var i = 0; i < that.data.listData1.length; i++) {
      if (i == digit) {
        that.data.listData1[i].amount  = that.data.listData1[i].amount  + 1
      }
      console.log('listData1['+i+'].acount改为'+that.data.listData1[i].amount);
    }
      that.data.summary_list[0].amount = 0;
      that.data.summary_list[0].weight = 0;
    for (var i = 0; i < that.data.listData1.length; i++) {
        that.data.summary_list[0].amount = that.data.summary_list[0].amount + that.data.listData1[i].amount;
        that.data.summary_list[0].weight = that.data.summary_list[0].weight + that.data.listData1[i].amount*that.data.weight_list[i].weight
        
      }
      that.data.summary_list[0].atmosphere = that.data.summary_list[0].weight/that.data.summary_list[0].amount;
     that.setData({
        message:"获得数据:"+str ,
        listData1:that.data.listData1,
        summary_list:that.data.summary_list
      })
    console.log('看看怎么显示上面这个');
    })
},
    
  })